/*
 * 原版接口
 */
import * as server from "@minecraft/server";
/*
 * 蛋白石数据
 */
import * as data_format from "../data/format";
/*
 * 蛋白石组件
 */
import * as derived_tools from "../system/derived_tools";
import * as native_tools from "../system/native_tools";
import * as achieve_plan from "../system/achieve_plan";
import * as math_tools from "../system/math_tools";

/**
 * * 渊鲸 - 执行者
 * 
 * @param {server.Entity} entity - 自身
 * 
 * @param {server.Entity} target - 目标
 * 
 * @param {boolean} erupt - 是否暴击
 */
export function Execute(entity: server.Entity, target: server.Entity, erupt: boolean) {
    /**
     * * 鱼雷爆炸事件
     * 
     * @param args - 附加参数
     */
    const powerExplode = (args: data_format.ROUTE_ANNEX_ARGS) => {
        // 验证 实体状态 是否正确
        if (!entity || !target || !entity.isValid() || !target.isValid()) return;
        /**
         * * 过滤器参数
         */
        const options: server.EntityQueryOptions = {
            excludeTypes: ["minecraft:item", "minecraft:xp_orb", entity.typeId],
            location: args.location,
            maxDistance: 6,
            closest: 8
        };
        /**
         * * 契约者标识符
         */
        const contract = entity?.getDynamicProperty('entity:contract_user') as string | undefined;
        /**
         * * 获取实体列表
         */
        const entitys = args.dimension.getEntities(options);
        // 过滤 契约者 与 锁定的目标
        entitys.filter(current => current.id !== contract && current.id !== entity.id);
        // 创建 元素伤害
        entitys.forEach(current => achieve_plan.RunePowerAttack.BriefCreate(entity, current, erupt));
        // 创建 粒子效果
        native_tools.TrySpawnParticle(args.dimension, 'constant:fireworks_fireball_rune_blue', args.location);
    };
    /**
     * * 设定路径点列表
     */
    const locationGroup = [
        entity.location,
        math_tools.Vector.add(entity.location, { x: 0, y: 5, z: 0 }),
        target.getHeadLocation()
    ];
    // 创建 路径执行器
    achieve_plan.PathExecute.Create('渊鲸潜艇-炮击射线', 1,
        {
            particles: ['constant:track_rune_blue'],
            location_group: locationGroup,
            dimension: entity.dimension,
            on_done: powerExplode,
            cooldown: 2,
            speed: 1
        }
    )
};
/**
 * * 渊鲸 - 君临者
 * 
 * @param {server.Entity} entity - 自身
 * 
 * @param {server.Entity} target - 目标
 * 
 * @param {boolean} erupt - 是否暴击
 */
export function Emperor(entity: server.Entity, target: server.Entity, erupt: boolean) {
    /**
     * * 鱼雷爆炸事件
     * 
     * @param args - 附加参数
     */
    const powerExplode = (args: data_format.ROUTE_ANNEX_ARGS) => {
        // 验证 实体状态 是否正确
        if (!entity || !target || !entity.isValid() || !target.isValid()) return;
        /**
         * * 过滤器参数
         */
        const options: server.EntityQueryOptions = {
            excludeTypes: ["minecraft:item", "minecraft:xp_orb", entity.typeId],
            location: args.location,
            maxDistance: 6,
            closest: 8
        };
        /**
         * * 获取实体列表
         */
        const entitys = args.dimension.getEntities(options).filter(current => current.id !== entity.id);
        // 遍历实体队列
        entitys.forEach(
            current => {
                // 50% 概率触发 潮涌能量 效果
                if (derived_tools.IsEnable(50)) current.addEffect('conduit_power', 600, { showParticles: false });
                // 创建 元素伤害
                achieve_plan.RunePowerAttack.BriefCreate(entity, current, erupt);
                // 判断 是否存在 [君王圣裁]标记
                if (!current.getDynamicProperty('whale_adjudication') || !(current instanceof server.Player)) return;
                // 添加 失明、黑暗、凋零 效果
                current.addEffect('blindness', 300, { showParticles: false });
                current.addEffect('darkness', 300, { showParticles: false });
                current.addEffect('wither', 300, { showParticles: false });
                current.addEffect('hunger', 300, { showParticles: false });
                // 播放 音效
                current.playSound('mob.guardian.attack_loop');
                current.playSound('mob.elderguardian.curse');
                // 清除 [君王圣裁] 标记
                current.setDynamicProperty('whale_adjudication');
            }
        );
        // 创建 粒子效果
        native_tools.TrySpawnParticle(args.dimension, 'constant:fireworks_fireball_rune_blue', args.location);
    };
    // 创建 路径执行器
    achieve_plan.PathExecute.Create('渊鲸潜艇-炮击射线', 1,
        {
            particles: ['constant:track_rune_blue'],
            location_group: [],
            dimension: entity.dimension,
            on_done: powerExplode,
            cooldown: 1,
            speed: 4,
            shoot: {
                start_place: entity.getHeadLocation(),
                toward: math_tools.Vector.difference(entity.location, target.location),
                max_distance: math_tools.Vector.distance(entity.location, target.location)
            }
        }
    )
};